//
//  BaseNavigationController.m
//  MainDemo
//
//  Created by FM on 2017/1/7.
//  Copyright © 2017年 FM. All rights reserved.
//

#import "BaseNavigationController.h"
#import "UIView+Extension.h"
#import "Define.h"
#import "Helper.h"
#import "UINavigationBar+Awesome.h"
@interface BaseNavigationController ()

@end

@implementation BaseNavigationController
/**
 * 当第一次使用这个类的时候会调用一次
 */
+ (void)initialize
{
    // 当导航栏用在NavigationController中, appearance设置才会生效
    //    UINavigationBar *bar = [UINavigationBar appearanceWhenContainedIn:[self class], nil];

//    UINavigationBar *bar = [UINavigationBar appearance];
//    [bar setBackgroundImage:[Helper creatImageFromColor:[UIColor whiteColor]] forBarMetrics:UIBarMetricsDefault];
    
     [[UINavigationBar appearance] setTitleTextAttributes:@{ NSFontAttributeName : Font_Semibold(18), NSForegroundColorAttributeName:UIColorFromRGB(0x111111)}];
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    attrs[NSForegroundColorAttributeName] = UIColorFromRGB(0x111111);
    
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSFontAttributeName] = attrs[NSFontAttributeName];
    selectedAttrs[NSForegroundColorAttributeName] =Color_fac444;
    //    selectedAttrs[NSForegroundColorAttributeName]=UIColorFromRGB(0x2a2a2a);
    
    UITabBarItem *item = [UITabBarItem appearance];
    [item setTitleTextAttributes:attrs forState:UIControlStateNormal];
    [item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
    [[UITabBar appearance] setBackgroundColor:[UIColor whiteColor]];
  
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //    [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
  //  [self.navigationBar lt_setBackgroundColor:[UIColor whiteColor]];
//    [self.view setBackgroundColor:[UIColor whiteColor]];
    // 如果滑动移除控制器的功能失效，清空代理(让导航控制器重新设置这个功能)
    //self.interactivePopGestureRecognizer.delegate = nil;
}

/**
 * 可以在这个方法中拦截所有push进来的控制器
 */
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
 
    if (self.childViewControllers.count>0) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:@"后退灰"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"后退灰"] forState:UIControlStateHighlighted];
        //button.size = CGSizeMake(70, 30);
        button.frame=CGRectMake(50, 0, 60, 44);
        // 让按钮内部的所有内容左对齐
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        //        [button sizeToFit];
        // 让按钮的内容往左边偏移5
        button.contentEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
        [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        
        // 修改导航栏左边的item
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
        viewController.hidesBottomBarWhenPushed = YES;
    }
        // 这句super的push要放在后面, 让viewController可以覆盖上面设置的leftBarButtonItem
        [super pushViewController:viewController animated:animated];
 
}
- (void)back
{
    [self popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
